package com.samsung.calendar;

import java.util.Calendar;

import com.samsung.R;

import android.app.Activity;
import android.app.DatePickerDialog;
import android.app.Dialog;
import android.app.DatePickerDialog.OnDateSetListener;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.DatePicker;
import android.widget.GridView;
import android.widget.TextView;

public class MonthlyCalendar extends Activity {
	
	private MonthlyCalendarAdapter mCalendarAdapter;
	private final int DATE_PICK_DIALOG = 1;
	private static final String INTENT_KEY_YYYYMM = "YYYYMM";
	private int mYear;
	private int mMonth;
	private int mDate;
	private TextView tv;
	private Button btn;

	
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.calendar_main);
		
        final Intent intent = getIntent();
        Bundle bundle = intent.getExtras();        
        Calendar cal = null;
        if (bundle != null)
        	cal = (Calendar)bundle.getSerializable(INTENT_KEY_YYYYMM);
        if (cal == null)
            cal = Calendar.getInstance();
                
        cal.set(Calendar.DATE, 1);        
		setCalendar(cal);
		
		setDateMoveButtonHandler(R.id.prevYear, Calendar.YEAR, -1);
		setDateMoveButtonHandler(R.id.prevMonth, Calendar.MONTH, -1);
		setDateMoveButtonHandler(R.id.nextYear, Calendar.YEAR, 1);
		setDateMoveButtonHandler(R.id.nextMonth, Calendar.MONTH, 1);
		
    }

	private void setCalendar(Calendar cal) {
    	
    	if (mCalendarAdapter == null)
    		mCalendarAdapter = new MonthlyCalendarAdapter(this, cal);
    	else
    		mCalendarAdapter.setBaseDate(cal);
    		
        btn = (Button) findViewById(R.id.prevYear);
        btn.setTag(cal);
        btn.setBackgroundResource(R.drawable.right_large);
        btn = (Button) findViewById(R.id.prevMonth);
        btn.setTag(cal);
        btn.setBackgroundResource(R.drawable.right_small);
        btn = (Button) findViewById(R.id.nextYear);
        btn.setTag(cal);
        btn.setBackgroundResource(R.drawable.left_large);
        btn = (Button) findViewById(R.id.nextMonth);
        btn.setTag(cal);
        btn.setBackgroundResource(R.drawable.left_small);

        tv = (TextView) findViewById(R.id.textview);
        
        String yyyyMM = String.format(getString(R.string.yearMonth)
				, Integer.toString(cal.get(Calendar.YEAR))
				, Integer.toString(cal.get(Calendar.MONTH)+1));
        tv.setTextColor(R.color.black);
        tv.setText(yyyyMM);

        tv.setOnClickListener(new View.OnClickListener() {
			
			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				Calendar cal = Calendar.getInstance();
				mYear = cal.get(Calendar.YEAR);
				mMonth = cal.get(Calendar.MONTH);
				mDate = cal.get(Calendar.DATE);
				showDialog(DATE_PICK_DIALOG);
			}
		});
        


        GridView gridview = (GridView) findViewById(R.id.gridview);
        gridview.setAdapter(mCalendarAdapter);
        

    }
    
    private void setDateMoveButtonHandler(int id, final int YearOrMonth, final int direction) {
        Button btn = (Button) findViewById(id);
		btn.setOnClickListener(new View.OnClickListener() {			
			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				Calendar cal = (Calendar)v.getTag();
				cal.add(YearOrMonth, direction);
				setCalendar(cal);
			}
		});
    	
    }
    
	@Override
	protected Dialog onCreateDialog(int id)
	{
		// TODO Auto-generated method stub
		switch(id)
		{
		case DATE_PICK_DIALOG :
			return new DatePickerDialog(this, new OnDateSetListener() {
				
				@Override
				public void onDateSet(DatePicker view, int year, int monthOfYear,
						int dayOfMonth)
				{
					// TODO Auto-generated method stub
					mYear = year;
					mMonth = monthOfYear;
					tv.setText(String.format(getString(R.string.yearMonth), mYear, mMonth+1));
				}
			},mYear,mMonth,mDate);
		}
		return null;
	}   
}